package org.shoukaiseki.supersaiyan.generator.domain;

import javax.validation.constraints.NotBlank;

import org.apache.ibatis.type.Alias;
import org.shoukaiseki.supersaiyan.common.core.domain.base.BaseEntity;
import org.shoukaiseki.supersaiyan.common.utils.StringUtils;
import org.shoukaiseki.supersaiyan.generator.domain.entity.GenTableColumnEntity;

/** org.shoukaiseki.supersaiyan.generator.domain.GenTableColumn
 * 代码生成业务字段表 gen_table_column
 * <br>
 * @author shoukaiseki
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 * @date 2022-07-31 22:30:57<br>
 */
@Alias("GenTableColumn")
public class GenTableColumn extends GenTableColumnEntity
{
    private static final long serialVersionUID = 1L;


    /** JAVA字段名 */
    @NotBlank(message = "Java属性不能为空")
    private String javaField;

    /** 是否主键（1是） */
//    private String isPk;

    /** 是否自增（1是） */
//    private String isIncrement;

    /** 是否必填（1是） */
//    private String isRequired;

    /** 是否为插入字段（1是） */
//    private String isInsert;

    /** 是否编辑字段（1是） */
//    private String isEdit;

    /** 是否列表字段（1是） */
//    private String isList;

    /** 是否查询字段（1是） */
//    private String isQuery;

    /** 查询方式（EQ等于、NE不等于、GT大于、LT小于、LIKE模糊、BETWEEN范围） */
//    private String queryType;

    @Override
    public String getJavaField() {
        return javaField;
    }

    @Override
    public void setJavaField(String javaField) {
        this.javaField = javaField;
    }

    /** 显示类型（input文本框、textarea文本域、select下拉框、checkbox复选框、radio单选框、datetime日期控件、image图片上传控件、upload文件上传控件、editor富文本控件） */
//    private String htmlType;




    public String getCapJavaField()
    {
        return StringUtils.capitalize(javaField);
    }


    public boolean isPk()
    {
        return isPk(this.getIsPk());
    }

    public boolean isPk(String isPk)
    {
        return isPk != null && StringUtils.equals("1", isPk);
    }


    public boolean isIncrement()
    {
        return isIncrement(this.getIsIncrement());
    }

    public boolean isIncrement(String isIncrement)
    {
        return isIncrement != null && StringUtils.equals("1", isIncrement);
    }


    public boolean isRequired()
    {
        return isRequired(this.getIsRequired());
    }

    public boolean isRequired(String isRequired)
    {
        return isRequired != null && StringUtils.equals("1", isRequired);
    }

    public boolean isInsert()
    {
        return isInsert(this.getIsInsert());
    }

    public boolean isInsert(String isInsert)
    {
        return isInsert != null && StringUtils.equals("1", isInsert);
    }

    public boolean isEdit()
    {
        return isInsert(this.getIsEdit());
    }

    public boolean isEdit(String isEdit)
    {
        return isEdit != null && StringUtils.equals("1", isEdit);
    }

    public boolean isList()
    {
        return isList(this.getIsList());
    }

    public boolean isList(String isList)
    {
        return isList != null && StringUtils.equals("1", isList);
    }


    public boolean isQuery()
    {
        return isQuery(this.getIsQuery());
    }

    public boolean isQuery(String isQuery)
    {
        return isQuery != null && StringUtils.equals("1", isQuery);
    }

    public boolean isSuperColumn()
    {
        return isSuperColumn(this.javaField);
    }

    public static boolean isSuperColumn(String javaField)
    {
        return StringUtils.equalsAnyIgnoreCase(javaField,
                // BaseEntity
                "createBy", "createTime", "updateBy", "updateTime", "remark",
                // TreeEntity
                "parentName", "parentId", "orderNum", "ancestors");
    }

    public boolean isUsableColumn()
    {
        return isUsableColumn(javaField);
    }

    public static boolean isUsableColumn(String javaField)
    {
        // isSuperColumn()中的名单用于避免生成多余Domain属性，若某些属性在生成页面时需要用到不能忽略，则放在此处白名单
        return StringUtils.equalsAnyIgnoreCase(javaField, "parentId", "orderNum", "remark");
    }

    public String readConverterExp()
    {
        String remarks = StringUtils.substringBetween(this.getColumnComment(), "（", "）");
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isNotEmpty(remarks))
        {
            for (String value : remarks.split(" "))
            {
                if (StringUtils.isNotEmpty(value))
                {
                    Object startStr = value.subSequence(0, 1);
                    String endStr = value.substring(1);
                    sb.append("").append(startStr).append("=").append(endStr).append(",");
                }
            }
            return sb.deleteCharAt(sb.length() - 1).toString();
        }
        else
        {
            return this.getColumnComment();
        }
    }

    public String getLabel(){
        if (StringUtils.isEmpty(this.getColumnComment())) {
            return StringUtils.EMPTY;
        }
        int parentheseIndex = this.getColumnComment().indexOf("（");
        if(parentheseIndex != -1){
            return this.getColumnComment().substring(0, parentheseIndex);
        }
        return this.getColumnComment();
    }
}
